﻿using System.ComponentModel;
using System.Reflection;

namespace 统一返回模型
{
    //定义返回状态码，用于判断返回结果是正常还是异常或者其他
    public enum ResultStatus
    {
        [Description("请求成功")]
        Success = 1,

        [Description("请求失败")]
        Fail = 0,

        [Description("请求异常")]
        Error = -1,
    }

    public class EnumHelper
    {
        /// <summary>
        /// 获取枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstend">当枚举没有定义的DescriptionAttribute，是否用枚举名代替，默认使用</param>
        /// <returns></returns>
        public static string GetDescription(Enum value, bool nameInstend = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attribute == null && nameInstend)
            {
                return name;
            }
            return attribute?.Description;
        }
    }

    //返回结果泛型包装类
    public class ResponseResult<T>
    {
        /// <summary>
        /// 状态结果
        /// </summary>
        public ResultStatus Status { get; set; } = ResultStatus.Success;

        private string? _msg;

        public string? Message
        {
            get { return !string.IsNullOrEmpty(_msg) ? _msg : EnumHelper.GetDescription(Status); }
            set { _msg = value; }
        }

        /// <summary>
        /// 返回结果
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 成功状态返回结果
        /// </summary>
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        public static ResponseResult<T> SuccessResult(T data)
        {
            return new ResponseResult<T> { Status = ResultStatus.Success, Data = data };
        }

        /// <summary>
        /// 失败状态返回结果
        /// </summary>
        /// <param name="msg">失败信息</param>
        /// <returns></returns>
        public static ResponseResult<T> FailResult(string? msg = null)
        {
            return new ResponseResult<T> { Status = ResultStatus.Fail, Message = msg };
        }

        /// <summary>
        /// 异常状态返回结果
        /// </summary>
        /// <param name="msg">异常信息</param>
        /// <returns></returns>
        public static ResponseResult<T> ErrorResult(string? msg = null)
        {
            return new ResponseResult<T> { Status = ResultStatus.Error, Message = msg };
        }

        /// <summary>
        /// 自定义状态返回结果
        /// </summary>
        /// <param name="status"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ResponseResult<T> Result(ResultStatus status, T data, string? msg = null)
        {
            return new ResponseResult<T> { Status = status, Data = data, Message = msg };
        }

        /// <summary>
        /// 隐式将T转化为ResponseResult<T>
        /// </summary>
        /// <param name="value"></param>
        public static implicit operator ResponseResult<T>(T value)
        {
            return new ResponseResult<T> { Data = value };
        }
    }
}